/**
 * 
 */
package com.diallom.bd;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.location.Location;

/**
 * @author diallom
 *
 */
public class ParkingDB extends SQLiteOpenHelper{
	private SQLiteDatabase db;
	
	public static final String TAG = "ParkingDB";
	public static final String DATABASE_NAME="parkings.db";
	public static final String KEY_ID = "_id";
	public static final String KEY_NAME = "name";
	public static final String KEY_NBPLACES = "nbplaces";
	public static final String KEY_STATIONNEMENT = "stationnement";
	public static final String KEY_NOTE = "note";
	public static final String KEY_LOCATION = "location";
	
	public static final String SQLITE_TABLE = "parking";
	private static final int DATABASE_VERSION = 1;
	
	

	public ParkingDB(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}
	public ParkingDB(Context ctx) {
		super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
		db = getWritableDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {		
		db.execSQL("CREATE TABLE " + SQLITE_TABLE + " ( " + KEY_ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME
				+ " TEXT NOT NULL," +KEY_NBPLACES + " INTEGER NULL," 
				+KEY_STATIONNEMENT + " TEXT NULL,"
				+KEY_NOTE + " FLOAT NULL,"
				+KEY_LOCATION + " TEXT NOT NULL);");
	}
	
	
	public void reinit() {
		if (db.isOpen())
			fermeture();

		db = getWritableDatabase();
		db.execSQL("DROP TABLE " + SQLITE_TABLE + ";");
		db.execSQL("CREATE TABLE " + SQLITE_TABLE + " ( " + KEY_ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME
				+ " TEXT NOT NULL," +KEY_NBPLACES + " INTEGER NULL," 
				+KEY_STATIONNEMENT + " TEXT NULL,"
				+KEY_NOTE + " REAL NULL,"
				+KEY_LOCATION + " TEXT NOT NULL);");
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	public Cursor getParkingsToCursor(){
		Cursor cursor = db.query(SQLITE_TABLE, null, null, null, null, null, "location");
		if(cursor != null)
			cursor.moveToFirst();
		return cursor;
	}
	
	public ArrayList<Parking> getParkingsToList(){
		ArrayList<Parking> list = new ArrayList<Parking>();
		Cursor cursor = db.query(SQLITE_TABLE, null, null, null, null, null, "location");
		if(cursor.getCount()==0)
			return list;
		else{
			cursor.moveToFirst();
			do{
				list.add(cursorToParking(cursor));
			}while(cursor.moveToNext());
			cursor.close();
			return list;
		}
	}
	
	
	public void fermeture() {
		db.close();
	}
	
	/**
	 * @param  Parking
	 * INSERT Parking in database
	 * @return id of parking in database
	 */
	public long addParking(Parking pkg){
		ContentValues valeurs = new ContentValues();
		
		valeurs.put(KEY_NAME, pkg.getNameParking());
		valeurs.put(KEY_NBPLACES, pkg.getNbOfPlaces());
		valeurs.put(KEY_STATIONNEMENT, pkg.getStationnement());
		valeurs.put(KEY_NOTE, pkg.getNote());
		valeurs.put(KEY_LOCATION, pkg.getLocation());
		
		return db.insert(SQLITE_TABLE, null, valeurs);
	}
	
	/**
	 * @param  Parking
	 * UPDATE the parking
	 * @return id of parking in database
	 */
	public long updateParking(Parking pkg){
		ContentValues valeurs = new ContentValues();
		valeurs.put(KEY_NAME, pkg.getNameParking());
		valeurs.put(KEY_NBPLACES, pkg.getNbOfPlaces());
		valeurs.put(KEY_STATIONNEMENT, pkg.getStationnement());
		valeurs.put(KEY_NOTE, pkg.getNote());
		valeurs.put(KEY_LOCATION, pkg.getLocation());
		
		return  db.update(SQLITE_TABLE, valeurs, KEY_ID + " = " + pkg.getId(),null);
	}
	/**
	 * @param  id
	 * DELETE the parking where id= @{id}
	 * @return id of parking in database
	 */
	public int deleteParking(int id) {
		return db.delete(SQLITE_TABLE, KEY_ID + " = " + id, null);
	}
	
	private Parking cursorToParking(Cursor cursor){
		Parking pkg = new Parking();
		pkg.setId(cursor.getInt(0));
		pkg.setNameParking(cursor.getString(1));
		pkg.setNbOfPlaces(cursor.getInt(2));
		pkg.setStationnement(cursor.getString(3));
		pkg.setNote(cursor.getInt(4));
		String s=cursor.getString(5);
		
		String[] tab=s.split(" ");
		Location location = new Location("");
		location.setLatitude(new Double(tab[0]));
		location.setLongitude(new Double(tab[1]));
		
		pkg.setLocation(location);
		return pkg;
	}
	
	public Parking getParkingById(int id){
		Cursor cursor = db.query(SQLITE_TABLE, null, KEY_ID + " = " + id,
				null, null, null, null);
		if (cursor.getCount() == 0)
			return null;
		else {
			cursor.moveToFirst();
			return cursorToParking(cursor);
		}
	}

}
